﻿using System;
using System.IO;
using BlueDot.Data.Core;
using BlueDot.Data.Core.Sql;

namespace CreateDatabaseFromEntities
{
    internal class Program
    {
        #region private

        private static void CreateDatabase()
        {
            Console.WriteLine("Create SqlCe Provider");
            ProviderFactory.DefaultProvider = new SqlCeProvider
                                                  {
                                                      ConnectionString = "Data Source = mfLYQuickStart.sdf"
                                                  };

            //By setting the provider log all commands executed on the provider will be sent 
            //to the specified stream
            ProviderFactory.DefaultProvider.Log = Console.Out;
            DataStoreSchemaBuilder.CreateDataStore(true);
        }

        private static void CreateDatabaseScripts()
        {
            Console.WriteLine("Writes all db schema scripts to file schemaScript.sql");
            ProviderFactory.DefaultProvider = new SqlCeProvider
                                                  {
                                                      ConnectionString = "Data Source = mfLYQuickStart.sdf"
                                                  };

            Console.WriteLine("Create database");

            ProviderFactory.DefaultProvider.Log = Console.Out;

            var fs = new FileStream("schemaScript.sql", FileMode.OpenOrCreate);
            StreamWriter writer = DataStoreSchemaBuilder.GenerateDDLCreateScripts(fs, true);
            writer.Flush();
            writer.Close();
        }

        private static void DeleteAndCreateDataStore()
        {
            ProviderFactory.DefaultProvider = new SqlCeProvider
                                                  {
                                                      ConnectionString = "Data Source = mfLYQuickStart.sdf"
                                                  };

            //By setting the provider log all commands executed on the provider will be sent 
            //to the specified stream
            ProviderFactory.DefaultProvider.Log = Console.Out;
            DataStoreSchemaBuilder.CreateDataStore(true);
        }

        private static void Main(string[] args)
        {
            CreateDatabase();

            CreateDatabaseScripts();

            MergeWithExisting();

            DeleteAndCreateDataStore();

            Console.ReadLine();
        }

        private static void MergeWithExisting()
        {
            Console.WriteLine("Merging with existing db");
            ProviderFactory.DefaultProvider = new SqlCeProvider
                                                  {
                                                      ConnectionString = "Data Source = mfLYQuickStart.sdf"
                                                  };

            ProviderFactory.DefaultProvider.Log = Console.Out;
            DataStoreSchemaBuilder.MergeDataStore(true);
        }

        #endregion
    }
}